//
//  CTTabBarItem.m
//  CustomTabbar
//
//  Created by
//  Copyright (c) 2015年 wulanzhou. All rights reserved.
//

#import "CTTabBarItem.h"
#import "Common.h"
#define degreesToRadians(x)(M_PI*x/180.0)

@implementation CTTabBarItem

@synthesize imageView;

@synthesize titleLb;

@synthesize normalImage;

@synthesize selectedImage;

- (instancetype)init
{
    if(self = [super init])
    {
        //do nothing
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width-30)/2, 10, 30, 30)];
        [self addSubview:imageView];
        
//        titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+3, self.frame.size.width, 10)];
//        titleLb.backgroundColor = [UIColor clearColor];
//        titleLb.font = [UIFont systemFontOfSize:10];
//        titleLb.textAlignment = NSTextAlignmentCenter;
//        titleLb.textColor = bgmColor(128, 128, 128);
//        [self addSubview:titleLb];
    }
    return self;
}

-(void)setNormalImage:(UIImage *)aNormalImage
        selectedImage:(UIImage *)aSelectedImage
                title:(NSString *)aTitle
{
    [self setNormalImage:aNormalImage selectedImage:aSelectedImage title:aTitle normalTitleColor:bgmColor(128, 128, 128) selectedTitleColor:[UIColor colorWithRed:0 green:174/255.0 blue:19/255.0 alpha:1]];
}

-(void)setNormalImage:(UIImage *)aNormalImage
        selectedImage:(UIImage *)aSelectedImage
                title:(NSString *)aTitle
     normalTitleColor:(UIColor *)aNormalColor
    selectedTitleColor:(UIColor *)aSelectedColor
{
    self.normalImage = aNormalImage;
    self.selectedImage = aSelectedImage;
    self.normalTitleColor = aNormalColor;
    self.selectedTitleColor = aSelectedColor;
    

    imageView.image = aNormalImage;
    titleLb.text = aTitle;
}
/*
 覆盖UIButton的setSelected方法
 */
- (void)setSelected:(BOOL)aSelected
{
    [self changeSelectedStatus:aSelected];
}

- (void)changeSelectedStatus:(BOOL)status{
    if(status)
    {
        imageView.image = self.selectedImage;
        titleLb.textColor = self.selectedTitleColor;
    }
    else
    {
        imageView.image = self.normalImage;
        titleLb.textColor = self.normalTitleColor;
    }
}


/**
 *  @author wulanzhou
 *
 *  @brief  动画旋转
 *
 *  @param view     传进来的view
 *  @param duration 动作
 *  @param wait     时间
 *  @param angle    旋转的角度/180
 */
- (void) rotate: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andAngle:(int) angle{
    __block BOOL done = wait; //wait =  YES wait to finish animation
    if (done == YES) {
        [UIView animateWithDuration:duration animations:^{
            view.transform = CGAffineTransformMakeRotation(degreesToRadians(angle));
        } completion:^(BOOL finished) {
            done = NO;
        }];
        // wait for animation to finish
        while (done == YES)
            [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
    }
    else
    {
        [UIView animateWithDuration:duration animations:^{
            view.transform = CGAffineTransformMakeRotation(-degreesToRadians(angle));
        } completion:^(BOOL finished) {
            done = NO;
        }];
        // wait for animation to finish
        while (done == YES)
            [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
        
    }
}
/**
 * 添加红色标记
 */
- (void)showBadge{
    
    if ([self viewWithTag:888]) {
        return;
    }
    
    UIView *badgeView = [[UIView alloc] init];
    
    badgeView.tag = 888;
    
    badgeView.layer.cornerRadius = 4.0f;
    badgeView.layer.masksToBounds = YES;
    
    badgeView.backgroundColor = bgmyelloeColor;
    
    CGRect tabFrame = self.frame;
    
    float percentX = 0.6;
    
    CGFloat x = percentX * tabFrame.size.width;
    
    CGFloat y = 0.1 * tabFrame.size.height;
    
    badgeView.frame = CGRectMake(x, y, 8.0f, 8.0f);
    
    [self addSubview:badgeView];
    
}
/**
 * 移除红色标记
 */
- (void)removeBadge{
    if ([self viewWithTag:888]) {
        [[self viewWithTag:888] removeFromSuperview];
    }
}

/**
 * 添加微章
 */
- (UIView *)showBadgeValue:(NSString*)badgeValue
{
    if ([badgeValue isKindOfClass:[NSNull class]]||badgeValue==nil||[badgeValue length]==0||[badgeValue isEqualToString:@"0"]) {
        [self removeBadgeValue];
        return nil;
    }
    UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width, 50)];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:0];
    
    item.badgeValue=badgeValue;
    NSArray *array = [[NSArray alloc] initWithObjects:item, nil];
    tabBar.items = array;
    //寻找
    
    for (UIView *viewTab in tabBar.subviews) {
        for (UIView *subview in viewTab.subviews) {
            NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)];
            if ([strClassName isEqualToString:@"UITabBarButtonBadge"] ||
                [strClassName isEqualToString:@"_UIBadgeView"]) {
                //从原视图上移除
                
                [subview removeFromSuperview];
                //
                
                [self addSubview:subview];
                float percentX =0.52;
                CGFloat y = ceilf(0.1 * self.frame.size.height);
                CGFloat x = ceilf(percentX * self.frame.size.width);
                subview.frame = CGRectMake(x,y,subview.frame.size.width, subview.frame.size.height);
                return subview;
            }
        }
    }
    return nil;
    
}

- (void)removeBadgeValue
{
    for (UIView *subview in self.subviews) {
        NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)];
        if ([strClassName isEqualToString:@"UITabBarButtonBadge"] ||
            [strClassName isEqualToString:@"_UIBadgeView"]) {
            [subview removeFromSuperview];
            break;
        }
    }
}

@end
